package cn.liurong.weixin.service.impl;

import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.dom.DOMElement;
import org.springframework.stereotype.Service;

import cn.liurong.weixin.service.MainService;
import cn.liurong.weixin.utils.SecretUtils;

/**
 * TODO: 增加描述
 * 
 * @author Administrator
 * @date 2017年4月27日 
 * @copyright wonhigh.cn 
 */
@Service("mainService")
public class MainServiceImpl implements MainService {
	private final static String token = "weixindemo";

	/**
	 * 1）将token、timestamp、nonce三个参数进行字典序排序
	 *	2）将三个参数字符串拼接成一个字符串进行sha1加密
	 *	3）开发者获得加密后的字符串可与signature对比，标识该请求来源于微信
	 */
	@Override
	public void tokenWx(HttpServletRequest request, HttpServletResponse response) {
		// 微信加密签名，signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
		String signature = request.getParameter("signature");
		// 时间戳
		String timestamp = request.getParameter("timestamp");
		// 随机数
		String nonce = request.getParameter("nonce");
		// 随机字符串
		String echostr = request.getParameter("echostr");

		// 1）将token、timestamp、nonce三个参数进行字典序排序
		String[] sortStringArr = { token, timestamp, nonce };
		Arrays.sort(sortStringArr);

		// 2）将三个参数字符串拼接成一个字符串进行sha1加密
		StringBuilder stringBuilder = new StringBuilder();
		for (String string : sortStringArr) {
			stringBuilder.append(string);
		}
		String sortString = stringBuilder.toString();
		String calcSignature = SecretUtils.SHA1(sortString);

		// 开发者获得加密后的字符串可与signature对比，标识该请求来源于微信
		if (!signature.equals(calcSignature)) {
			System.out.println("验证签名不通过");
			echostr = "false";
		} else {
			System.out.println("验证签名通过");
		}

		try {
			response.getWriter().append(echostr).close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	@Override
	public void sendMessage(Map<String, String> messageMap, HttpServletRequest request, HttpServletResponse response,
			String content) {
		Document document = DocumentHelper.createDocument();

		Element root = document.addElement("xml");

		Element chirld = new DOMElement("ToUserName");
		chirld.setText(messageMap.get("FromUserName"));
		root.add(chirld);
		chirld = new DOMElement("FromUserName");
		chirld.setText(messageMap.get("ToUserName"));
		root.add(chirld);
		chirld = new DOMElement("CreateTime");
		chirld.setText(System.currentTimeMillis() + "");
		root.add(chirld);
		chirld = new DOMElement("MsgType");
		chirld.setText("text");
		root.add(chirld);
		chirld = new DOMElement("Content");
		chirld.setText(content);
		root.add(chirld);

		try {
			String asXML = document.asXML();
			System.out.println(asXML);
			response.getWriter().write(asXML);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void sendImageMessage(Map<String, String> messageMap, HttpServletRequest request,
			HttpServletResponse response) {
		Document document = DocumentHelper.createDocument();

		Element root = document.addElement("xml");

		Element chirld = new DOMElement("ToUserName");
		chirld.setText(messageMap.get("FromUserName"));
		root.add(chirld);
		chirld = new DOMElement("FromUserName");
		chirld.setText(messageMap.get("ToUserName"));
		root.add(chirld);
		chirld = new DOMElement("CreateTime");
		chirld.setText(System.currentTimeMillis() + "");
		root.add(chirld);
		chirld = new DOMElement("MsgType");
		chirld.setText("image");
		root.add(chirld);
		chirld = new DOMElement("Image");
		Element chirldchirld = new DOMElement("MediaId");
		chirldchirld.setText(messageMap.get("MediaId"));
		chirld.add(chirldchirld);
		root.add(chirld);

		try {
			String asXML = document.asXML();
			System.out.println(asXML);
			response.getWriter().write(asXML);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
